#ifndef __Card_SPI_C
#define __Card_SPI_C

#include "Card_SPI.h"

void CardSPI_init(){
	  GPIO_Config_T  configStruct;
	  SPI_Config_T spiConfig;
		
    RCM_EnableAPB2PeriphClock(CardSPI_Pin_RCM);
   
	  // SPI引脚初始化：
	  // SCLK MOSI
	  configStruct.pin = CardSPI_SCLK_PinId | CardSPI_MOSI_PinId;           
	  configStruct.mode = GPIO_MODE_AF_PP; 
    #if defined AT32F403AVGT7  && card_spi_pullup > 0  
	  configStruct.gpio_pull = GPIO_PULL_UP; 
	  #endif 	
	  GPIO_Config(CardSPI_PinPort, &configStruct);       
	  // MISO
	  configStruct.pin = CardSPI_MISO_PinId;           
	  configStruct.mode = GPIO_MODE_IN_FLOATING;
    #if defined AT32F403AVGT7  && card_spi_pullup > 0
	  configStruct.gpio_pull = GPIO_PULL_UP; 
	  #endif 	
	  GPIO_Config(CardSPI_PinPort, &configStruct);
		// CS
		configStruct.pin = CardSPI_CS_PinId;           
	  configStruct.mode = GPIO_MODE_OUT_PP; 
    #if defined AT32F403AVGT7 && card_spi_pullup > 0
	  configStruct.gpio_pull = GPIO_PULL_UP; 
	  #endif 		
	  GPIO_Config(CardSPI_PinPort, &configStruct);		
	  
	  // SPI功能初始化：    
    RCM_EnableAPB1PeriphClock(CardSPI_RCM);  		
	  SPI_ConfigStructInit(&spiConfig);
    // 字节长度
    spiConfig.length = SPI_DATA_LENGTH_8B;
    // 波特率分频：64分频;
    
    #ifdef AT32F403AVGT7		
    spiConfig.baudrateDiv = SPI_BAUDRATE_DIV_64;
		#else
		spiConfig.baudrateDiv = SPI_BAUDRATE_DIV_64;
		#endif
    		
    // 两线全双工
    spiConfig.direction = SPI_DIRECTION_2LINES_FULLDUPLEX;
    // 高位先行:
    spiConfig.firstBit = SPI_FIRSTBIT_MSB; //SPI_FIRSTBIT_LSB
    // 主设备模式：
    spiConfig.mode = SPI_MODE_MASTER;
    // SPI时钟极性CPOL = 1：闲时高电平。
    spiConfig.polarity = SPI_CLKPOL_LOW;
    // SPI片选模式选择：软件片选 or 硬件片选
    spiConfig.nss = SPI_NSS_SOFT;
    // SPI时钟相位CPHA = 1 = 第1个沿是写：采集数据是第2边沿。
    spiConfig.phase = SPI_CLKPHA_1EDGE;
    // SPI config： 
    SPI_Config(CardSPI_id, &spiConfig);
    // 失能校验和
    SPI_DisableCRC(CardSPI_id);
		
    // 使能SPI；
    SPI_Enable(CardSPI_id);
		
		// CS初始化为高电平
		CardSPI_bit_set(CardSPI_CS_PinId);
		
		#ifdef SYS_DEBUG
		printf("   CardSPI_init finish   \r\n");
		#endif
}

#ifdef CardSPI_RST_PinId
void CardSPI_RST_init(){
	  GPIO_Config_T  configStruct;
	  
	  #ifdef CardSPI_RST_RCM
    RCM_EnableAPB2PeriphClock(CardSPI_RST_RCM); 
	  #else
	  RCM_EnableAPB2PeriphClock(CardSPI_Pin_RCM);
    #endif	
	
	  // RST复位初始化：
	  configStruct.pin = CardSPI_RST_PinId;  
    configStruct.mode = GPIO_MODE_OUT_PP;  
    configStruct.speed = GPIO_SPEED_50MHz;   
    
	  #ifdef CardSPI_RSTPort
    GPIO_Config(CardSPI_RSTPort, &configStruct);
	  #else
	  GPIO_Config(CardSPI_PinPort, &configStruct);
    #endif
	  
	  // RST初始化
	  CardSPI_RST_High();
  	
		#ifdef SYS_DEBUG
		printf("   CardSPI_RST_init   \r\n");     
		#endif		
}
#endif

uint8_t CardSPI_SendRead_Byte(uint8_t data){   
	  uint16_t timeout = 0xffff;	  
	  while (SPI_I2S_ReadStatusFlag(CardSPI_id, SPI_FLAG_TXBE) == RESET && timeout--){
			  
		}
    SPI_I2S_TxData(CardSPI_id, data); 		
		timeout = 0xffff;
	  while (SPI_I2S_ReadStatusFlag(CardSPI_id, SPI_FLAG_RXBNE) == RESET && timeout--){ 
			  
		}		
    return (uint8_t)SPI_I2S_RxData(CardSPI_id);
}


#endif

